package cn.aylog.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 获取本地磁盘上的配置文件
 */
@Configuration
public class JdbcCfg {

    private static String driverClass;
    private static String url;
    private static String username;
    private static String password;

    // 加载本地数据库配文件
    static {
        Properties sys = System.getProperties();
        String home = sys.getProperty("user.home");
        try(InputStream is = new FileInputStream(home + "/db.properties")) {
            Properties pro = new Properties();
            pro.load(is);
            driverClass = pro.getProperty("driverClass");
            url = pro.getProperty("url");
            username = pro.getProperty("username");
            password = pro.getProperty("password");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 将读到的属性注册到spring，便于使用
     */

    @Bean("driverClass")
    public static String getDriverClass() {
        return driverClass;
    }

    @Bean("url")
    public static String getUrl() {
        return url;
    }

    @Bean("username")
    public static String getUsername() {
        return username;
    }

    @Bean("password")
    public static String getPassword() {
        return password;
    }
}
